package com.ct.redis.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@ConditionalOnProperty("spring.redis.host")
@Configuration
@Slf4j
public class RedissonConfiguration {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private String port;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.connectionMinimumIdleSize:24}")
    private int connectionMinimumIdleSize;

    @Value("${spring.redis.connectionPoolSize:64}")
    private int connectionPoolSize;

//	@Value("${spring.redis.cluster.nodes:}")
//	private String nodes;

    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();

        //集群
//		if (StringUtils.isNotBlank(nodes)) {
//			ClusterServersConfig serverConfig = config.useClusterServers();
//			serverConfig.setScanInterval(2000); // 集群状态扫描间隔时间，单位是毫秒
//			if (StringUtils.isNotEmpty(nodes)) {
//				String str[] = nodes.split(",");
//				for (int i = 0; i < str.length; i++) {
//					log.info("redis addr : [{}]", str[i]);
//					if (StringUtils.isNotEmpty(str[i])) {
//						serverConfig.addNodeAddress("redis://" + str[i].trim());//// use "rediss://" for SSL connection
//					}
//				}
//			} else {
//				log.error("REDIS集群地址为空,系统退出");
//				System.exit(0);
//			}
//		}
        //单机
//		else {
        SingleServerConfig serverConfig = config.useSingleServer();
        serverConfig.setPingConnectionInterval(0);// 防止出现 redis 连接不上的问题
        serverConfig.setDnsMonitoringInterval(-1);
        serverConfig.setConnectionPoolSize(connectionPoolSize);
        //serverConfig.setConnectionMinimumIdleSize(connectionMinimumIdleSize);
        serverConfig.setDnsMonitoringInterval(10000);

        serverConfig.setAddress("redis://" + host + ":" + port);
        if (StringUtils.isNotEmpty(password)) {
            serverConfig.setPassword(password);
        }
//		}

        return Redisson.create(config);

    }

}
